package cn.jtfadmin.sysmonitor.domain.dto;

import lombok.Data;
import oshi.util.FormatUtil;

import java.io.Serializable;
import java.util.List;

/**
 * 磁盘信息DTO
 */
@Data
public class DiskInfoDTO implements Serializable {

    /**
     * 磁盘名称
     */
    protected String name;

    /**
     * 磁盘型号
     */
    protected String model;

    /**
     * 磁盘序列号
     */
    protected String serial;


    /**
     * 磁盘大小，字节
     */
    protected long size;

    /**
     * 磁盘大小， 可视化名字
     */
    protected String displaySize;

    /**
     * 磁盘读次数
     */
    protected long reads;


    /**
     * 磁盘读速率 字节
     */
    protected long readBytes;

    /**
     * 磁盘读速率 可视化名字
     */
    protected String displayReadBytes;


    /**
     * 磁盘写次数
     */
    protected long writes;


    /**
     * 磁盘写速率 字节
     */
    protected long writeBytes;

    /**
     * 磁盘写速率 可视化名字
     */
    protected String displayWriteBytes;

    /**
     * 磁盘读写次数/毫秒
     */
    protected long transferTime;


    /**
     * 分区信息
     */
    protected List<PartitionDTO> partitions;

    @Override
    public String toString() {
        boolean readwrite = getReads() > 0 || getWrites() > 0;
        StringBuilder sb = new StringBuilder();
        sb.append(getName()).append(": ");
        sb.append("(model: ").append(getModel());
        sb.append(" - S/N: ").append(getSerial()).append(") ");
        sb.append("size: ").append(getSize() > 0 ? FormatUtil.formatBytesDecimal(getSize()) : "?").append(", ");
        sb.append("reads: ").append(readwrite ? getReads() : "?");
        sb.append(" (").append(readwrite ? FormatUtil.formatBytes(getReadBytes()) : "?").append("), ");
        sb.append("writes: ").append(readwrite ? getWrites() : "?");
        sb.append(" (").append(readwrite ? FormatUtil.formatBytes(getWriteBytes()) : "?").append("), ");
        sb.append("xfer: ").append(readwrite ? getTransferTime() : "?");
        return sb.toString();
    }

    /**
     * 分区信息
     */
    @Data
    public static class PartitionDTO implements Serializable{

        /**
         * 分区编号
         */
        private  String identification;

        /**
         * 分区名称
         */
        private  String name;

        /**
         * 分区格式
         */
        private  String type;

        /**
         * 分区uuid
         */
        private  String uuid;

        /**
         * 分区大小 字节
         */
        private  long size;


        /**
         * 分区可视化大小
         */
        private String displaySize;

        /**
         * Device ID (Major)
         */
        private  int major;

        /**
         * Device ID (Minor)
         */
        private  int minor;

        /**
         * 挂载点
         */
        private  String mountPoint;


        public PartitionDTO(String identification, String name, String type, String uuid, long size, String displaySize, int major, int minor, String mountPoint) {
            this.identification = identification;
            this.name = name;
            this.type = type;
            this.uuid = uuid;
            this.size = size;
            this.displaySize = displaySize;
            this.major = major;
            this.minor = minor;
            this.mountPoint = mountPoint;
        }

        public PartitionDTO() {
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(getIdentification()).append(": ");
            sb.append(getName()).append(" ");
            sb.append("(").append(getType()).append(") ");
            sb.append("Maj:Min=").append(getMajor()).append(":").append(getMinor()).append(", ");
            sb.append("size: ").append(FormatUtil.formatBytesDecimal(getSize()));
            sb.append(getMountPoint().isEmpty() ? "" : " @ " + getMountPoint());
            return sb.toString();
        }
    }
}
